<?php
/**
 * Created by PhpStorm.
 * User: Keter
 * Date: 2016/9/2
 * Time: 9:55
 */
class GameServer extends DatabaseBase
{
    /**
     * 公用服务器列表Key
     * @var string
     */
    public static $publicServerKey = 'publicServer';

    /**
     * 公用服务器
     * @var int
     */
    public static $serverPublic = '0';

    /**
     * 专用服务器
     * @var int
     */
    public static $serverSpecial = '1';

    /**
     * 服务器字段名
     * @var array
     */
    public static $fields = [
        'serverID', 'type', 'domain', 'ip', 'lanIP', 'serverPort',
        'agentPort', 'onlineLimit', 'createTime', 'creator',
        'modifyTime', 'modifier'
    ];

    /**
     * 选择一个服务器
     * @param $channelId
     * @return null
     */
    public function selectServer($channelId)
    {
        $result = $this->redis->hMget('CHA:'. $channelId, ['serverID']);
        $this->i('get_server', $result);
        $serverID = 0;

        if (!$result) {
            $this->returnError('no_channel_info', -1, $channelId);
        }

        if ($result['serverID']) { //渠道专用服务器
            $serverID = $result['serverID'];
            if (!$this->checkServerOpen($serverID)) {
                $this->returnError('is_closed_server', -1, ['serverID' => $serverID]);
            }
        } else { //公用服务器
            $publicServerList = $this->redis->sMembers('publicServer');
            shuffle($publicServerList);
            if ($publicServerList) {
                $redis = $this->redis('status');
                foreach ($publicServerList as $key => $value) {
                    if ($this->checkServerOpen($value)) {
                        $statusInfo = $redis->hMget('serverStatus:' . $value, ['overread']);
                        if (is_numeric($statusInfo['overread']) && $statusInfo['overread'] === '0') {
                            $serverID = $value;
                            break;
                        }
                    }
                }
            }
        }

        if (! $serverID) {
            $this->returnError('no_server_id');
        }

        return $serverID;
    }

    /**
     * 检测服务器是否开启
     * @param $serverID
     * @return bool
     */
    public function checkServerOpen($serverID)
    {
        $ret = $this->redis->hMget('S:' . $serverID, ['isOpen']);
        return $ret['isOpen'] ? true : false;
    }

    /**
     * 检测服务器是否存在
     * @return mixed
     */
    public function existsServer($serverID)
    {
        return $this->redis->exists('S:' . $serverID);
    }

    /**
     * 获取服务器地址
     * @return array
     */
    public function getServerAddress($serverID)
    {
        $serverInfo = $this->redis->hMget(
            'S:' . $serverID,
            array('serverID', 'domain', 'ip', 'serverPort', 'agentPort')
        );

        if ($serverInfo['ip']) {
            return array(
                'domain' => $serverInfo['domain'],
                'ip' => $serverInfo['ip'],
                'lanIP' => $serverInfo['lanIP'],
                'serverPort' => $serverInfo['serverPort'],
                'agentPort' => $serverInfo['agentPort']
            );
        }

        return array();
    }

    /**
     * 获取服务器信息
     * @param $serverID
     * @param array $fields
     * @return mixed
     */
    public function getServerInfo($serverID, array $fields = [])
    {
        $key = 'S:' . $serverID;
        $exists = $this->redis->exists($key);
        if (!$exists) {
            $this->returnError('no_server');
        }

        if (empty($fields)) {
            $fields = self::$fields;
        }

        $result = $this->redis->hMget($key, $fields);
        return $result;
    }

    /**
     * 获取测试人员专用服务器配置
     * @return int
     */
    public function getTestServer($uid)
    {
        if (property_exists('TestConfig', 'test_servers')) {
            $testServers = TestConfig::$test_servers;
            if (isset($testServers[$uid])) {
                return $testServers[$uid];
            }
        }

        return 0;
    }
}